package com.cungmua.app.task;

import android.content.Context;

import com.cungmua.app.handler.BaseAuthenticatedRequestHandler;
import com.cungmua.app.handler.LikeHandler;
import com.cungmua.app.model.request.LikeRequest;
import com.cungmua.app.model.response.LikeResponse;
import com.cungmua.app.net.CungMuaAPI;

import retrofit.RetrofitError;

/**
 * Created by kelvin.nguyen on 6/26/14.
 */
public class LikeTask extends AuthenticatedTask<LikeRequest, Void, LikeResponse> {
    private Context context;
    private LikeHandler handler;

    public LikeTask(Context context, LikeHandler handler) {
        this.context = context;
        this.handler = handler;
    }

    @Override
    protected Context getContext() {
        return context;
    }

    @Override
    protected LikeResponse doInBackground() throws RetrofitError {
        LikeRequest request = params[0];

        return CungMuaAPI.getService().likeDeal(request.toFieldMap());
    }

    @Override
    protected BaseAuthenticatedRequestHandler getHandler() {
        return handler;
    }

    @Override
    protected void onSuccess(LikeResponse likeResponse) {
        likeResponse.setLiked(params[0].isLiked() == 1);
        handler.onLiked(likeResponse);
    }
}
